﻿using HualongExcel.Enum;
using HualongExcel.Utilities;
using Microsoft.VisualBasic;
using System;
using System.Windows.Forms;

namespace HualongExcel.Froms
{
    public partial class WorktemplateForm : Form
    {

        private static WorktemplateForm formIns;

        public static WorktemplateForm FormIns
        {
            get
            {

                if (formIns == null || formIns.IsDisposed)
                    formIns = new WorktemplateForm();
                return formIns;
            }
        }

        public static void ShowSignleForm(bool isDialogue)
        {


            if (isDialogue)
                FormIns.ShowDialog();
            else
                FormIns.Show();

            FormIns.Refersh();

        }
        public static void HideSignleForm()
        {
            FormIns.Hide();
        }

        public static void DisposeSignleForm()
        {
            FormIns.Dispose();
        }



        public WorktemplateForm()
        {
            InitializeComponent();
            Refersh();
        }

        private void WorktemplateForm_Load(object sender, EventArgs e)
        {
            this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;//桌面的宽度的一半减去自身宽的的一半
            this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;//桌面的高度的一半减去自身高度的一半
            Refresh();
        }


        private void setupButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择目标模板文件";
            dialog.Filter = "json文件(*.json)|*.json";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Common.WorktemplatePath = dialog.FileName;
                if (SaveHelper.SetData(WorkTemplateConstant.Worktemplatepath.ToString(), Common.WorktemplatePath))
                {
                    MessageBox.Show("设置目标模板成功：" + dialog.FileName);
                }
                Refersh();
            }
        }

        private void Refersh()
        {
            if (!String.IsNullOrWhiteSpace(Common.WorktemplatePath) && Common.WorktemplateData != null)
            {
                templateNameLabel.Text = $"模板名称：{Common.WorktemplateData.templateName}";
                templatePathLabel.Text = $"模板路径：{Common.WorktemplateData.templatePath}";
                GenerateCountLabel.Text = $"生成次数：{SaveHelper.GetData(RibbonConstant.GenerateCount.ToString(), 0)}";
            }
            else
            {
                templateNameLabel.Text = $"模板名称：无";
                templatePathLabel.Text = $"模板路径：无";
                GenerateCountLabel.Text = $"生成次数：读取失败";
            }

        }

        private void ClearWorkTemplateButton_Click(object sender, EventArgs e)
        {
            Common.WorktemplatePath = String.Empty;
            if (SaveHelper.SetData(WorkTemplateConstant.Worktemplatepath.ToString(), Common.WorktemplatePath))
            {
                MessageBox.Show("清除目标模板成功");
            }
            Refersh();
        }

        private void ResetButton_Click(object sender, EventArgs e)
        {
           
            string msg = "确定需要重置生成次数吗？\n这会影响生成的序列号的顺序";

            if ((int)MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != 1) { return; }

            if (SaveHelper.SetData(RibbonConstant.GenerateCount.ToString(), 0))
            {
                MessageBox.Show("重置生成次数成功");
            }
            Refersh();
        }

        private void ChangeButton_Click(object sender, EventArgs e)
        {
            string msg = "确定需要修改生成次数吗？\n这会影响生成的序列号的顺序";

            if ((int)MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != 1) { return; }
            this.TopMost = false;
            var countText= Interaction.InputBox("请输入要修改的生成次数", "修改生成次数", Common.GenerateCount.ToString(), -1, -1);
            this.TopMost = true;
            try
            {
                Common.GenerateCount = Convert.ToInt32(countText);
                Common.SaveGenerateCount();
                MessageBox.Show($"修改成功\n生成次数已修改为：{Common.GenerateCount}次");
            }
            catch
            {
                MessageBox.Show("修改错误，输入的次数不合法");
            }
            Refersh();
        }
    }
}
